function HandlePageAcctExpense(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._createElements();
};

HandlePageAcctExpense.prototype._createElements = function() {
  var _self = this;
  this._popupBox = new PopupBox();
  this._popuopBox2 = new PopupBox2();
  
  this._expense = Expense.instance();
  this._expense.payDate = '';
  this._search = DlmanageUtils.acctExpenseSearch();
  
  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.backgroundColor = '#fff';
  td.colSpan = 11;
  td.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandlePageAcctExpense.prototype._loadData = function() {
  this._results = null;
  this._payees = null;
  this._expenses = null;
  this._particulars = null;
  this._parameters = null;
  this._retrieveExpenses(1);
  this._retrieveResults(1);
  this._retrievePayees();
  this._retrieveParticulars();
};

HandlePageAcctExpense.prototype._verifyData = function() {
  if (this._results && this._expenses && this._payees && this._particulars) {
    this._updateElements();
  }
};

HandlePageAcctExpense.prototype._retrieveResults = function(page, condition, from, to, pfrom, pto, pos) {
  var _self = this;
  var args = ((condition) ?  'c=' + condition : '') + '&p=' + page + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '') + ((pfrom) ?  '&pfrom=' + pfrom : '') + ((pto) ?  '&pto=' + pto : '');
  new RequestUtils()._custom('acctexpense', args, function(result, params) { _self._results = result;
                                                                             _self._verifyData.call(_self);
                                                                           }, { pos: (pos) ? pos : null });
};

HandlePageAcctExpense.prototype._retrieveExpenses = function(page, condition, from, to, pfrom, pto, pos) {
  var _self = this;
  var args = ((condition) ?  'c=' + condition : '') + '&p=' + page  + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '') + '&s=' + DlmanageUtils.number + ((pfrom) ?  '&pfrom=' + pfrom : '') + ((pto) ?  '&pto=' + pto : '');
  new RequestUtils()._custom('acctexpenses', args, function(result, params) { _self._expenses = result.data;
                                                                              _self._parameters = result;
                                                                              _self._verifyData.call(_self);
                                                                            }, { pos: (pos) ? pos : null });
};

HandlePageAcctExpense.prototype._retrievePayees = function() {
  var _self = this;
  new RequestUtils()._read('ipayee', null, null, null, null, 'd.name', null, function(result, params) { _self._payees = result.data;
                                                                                                        _self._verifyData.call(_self);
                                                                                                      }, null);
};

HandlePageAcctExpense.prototype._retrieveParticulars = function() {
  var _self = this;
  new RequestUtils()._read('iparticular', null, null, null, null, 'd.name', null, function(result, params) { _self._particulars = result.data;
                                                                                                             _self._verifyData.call(_self);
                                                                                                           }, null);
};

HandlePageAcctExpense.prototype._updateElements = function() {
  var _self = this;
  DlmanageUtils.clear(this._gui.summarize);
  DlmanageUtils.clear(this._gui.search);
  DOMUtils.removeChildElements(this._gui.pagination);
  DlmanageUtils.clear(this._gui.add);
  DOMUtils.removeTableRows(this._gui.mains, 0);
  

  this._gui.summarize.expense.appendChild(document.createTextNode('$' + (this._results.expense).toFixed(2)));
  this._gui.summarize.cash.appendChild(document.createTextNode('$' + (this._results.cash).toFixed(2)));
  this._gui.summarize.noncash.appendChild(document.createTextNode('$' + (this._results.noncash).toFixed(2)));
  
  this._expense.userTrackId = this._operator.trackId;
  this._expense.userAccount = this._operator.account;
  this._gui.add.createdDate.appendChild(document.createTextNode(SimpleDate.format(this._now)));
  this._expense.createdDate = this._now;
  
  this._gui.add.paidDate.value = (this._expense.payDate) ? SimpleDate.format(this._expense.payDate) : '';
  this._gui.add.paidDate.onclick = function() { var context = this;
                                                _self._changeDate.call(_self, this, _self._expense.payDate, function(sd) { _self._expense.payDate = sd; context.focus(); }, false);
                                                return false;
                                              };
  this._gui.add.payee.who.options[this._gui.add.payee.who.options.length] = new Option('');
  for (var i = 0, il = this._payees.length; i < il; i++) {
    var payee = this._payees[i];
    var option = new Option(payee.name);
    this._gui.add.payee.who.options[this._gui.add.payee.who.options.length] = option;
    if (option.text == this._expense.payee){
      this._gui.add.payee.who.selectedIndex = this._gui.add.payee.who.options.length - 1;
    }
  }
  if (this._payees.length > 0) {
    this._expense.payee = this._gui.add.payee.who.options[this._gui.add.payee.who.selectedIndex].text;
    this._gui.add.payee.who.onchange = function() { _self._expense.payee = this.options[this.selectedIndex].text; };
  }
  this._gui.add.payee.add.onclick = function() { var object = IPayee.instance();
                                                 var div = document.createElement('div');
                                                 var func1 = function() { _self._popupBox._close();
                                                                          _self._expense.payee = object.name;
                                                                          new RequestUtils()._write('ipayee', [object], [], function() { _self._retrievePayees.call(_self); }, { pos: DOMUtils.findPos(_self._gui.add.payee.add) });
                                                                        };
                                                 var func2 = function() { _self._popupBox._close();
                                                                        };
                                                 MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                 new DialogIObject(div, object, { name: 'ipayee', title: 'Payees' });
                                               };

  this._gui.add.particulars.particular.options[this._gui.add.particulars.particular.options.length] = new Option('');
  for (var i = 0, il = this._particulars.length; i < il; i++) {
   var particular = this._particulars[i];
   var option = new Option(particular.name);
   this._gui.add.particulars.particular.options[this._gui.add.particulars.particular.options.length] = option;
   if (option.text == this._expense.particular){
     this._gui.add.particulars.particular.selectedIndex = this._gui.add.particulars.particular.options.length - 1;
   }
 }
 if (this._particulars.length > 0) {
   this._expense.particular = this._gui.add.particulars.particular.options[this._gui.add.particulars.particular.selectedIndex].text;
   this._gui.add.particulars.particular.onchange = function() { _self._expense.particular = this.options[this.selectedIndex].text; };
 }
 this._gui.add.particulars.add.onclick = function() { var object = IParticular.instance();
                                                      var div = document.createElement('div');
                                                      var func1 = function() { _self._popupBox._close();
                                                                               _self._expense.particular = object.name;
                                                                               new RequestUtils()._write('iparticular', [object], [], function() { _self._retrieveParticulars.call(_self); }, { pos: DOMUtils.findPos(_self._gui.add.particulars.add) });
                                                                             };
                                                      var func2 = function() { _self._popupBox._close();
                                                                             };
                                                      MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                      new DialogIObject(div, object, { name: 'iparticular', title: 'Particulars' });
                                                    };
  this._gui.add.c.checked = false;
  this._gui.add.b.checked = false;
  this._gui.add.q.checked = false;
  this._gui.add.c.onclick = function() { _self._expense.method = 'C';};
  this._gui.add.b.onclick = function() { _self._expense.method = 'B';};
  this._gui.add.q.onclick = function() { _self._expense.method = 'Q';};

  this._gui.add.amount.onchange = function() { _self._expense.amount = this.value; };
  this._gui.add.qn.onchange = function() { _self._expense.chequeNumber = this.value; };
  
  this._gui.add.ng.onclick = function() { if (this.checked == true) {
                                            _self._expense.ng = true;
                                          } else {
                                            _self._expense.ng = false;
                                          }
                                        };
  this._gui.add.ne.onclick = function() { if (this.checked == true) {
                                            _self._expense.ne = true;
                                          } else {
                                            _self._expense.ne = false;
                                          }
                                        };
  this._gui.add.note.onchange = function() { _self._expense.note = this.value; };
  this._gui.add.add.onclick = function() { if (!_self._expense.payDate) {
                                             window.alert('请填写付款时间');
                                             return;
                                           } else if(!_self._expense.payee) {
                                             window.alert('请选择收款人');
                                             return;
                                           } else if(!_self._expense.particular) {
                                             window.alert('请选择支付项目');
                                             return;
                                           } else if (_self._expense.amount== '' || _self._gui.add.amount.value == 0) {
                                             window.alert('数值不能为\'' + _self._gui.add.amount.value + '\'');
                                             return;
                                           } else if (!_self._expense.method) {
                                             window.alert('请选择付款类型');
                                             return;
                                           } else if (_self._expense.method == 'Q' && !_self._expense.chequeNumber) {
                                             window.alert('请填写支票编号');
                                             return;
                                           } else {
                                             new RequestUtils()._write('expense', [_self._expense], [], function() { location.reload(); }, { pos: DOMUtils.findPos(this) });
                                           }
                                         };
  
  if (this._expenses.length == 0) {
    tr = this._gui.mains.insertRow(-1);
    tr.className = 'roweven';
    td = tr.insertCell(-1);
    td.colSpan = 11;
    td.style.height = '24px';
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }

  for (var i = 0, il = this._expenses.length; i < il; i++) {
    var expense = this._expenses[i];
    tr = this._gui.mains.insertRow(-1);
    tr.className = (tr.rowIndex % 2 == 0) ? 'roweven' : 'rowodd';
    td = tr.insertCell(-1);
    td.style.height = '25px';
    td.style.textAlign = 'center';
    td.style.width = '69px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(SimpleDate.format(expense.createdDate)));
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.width = '69px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(SimpleDate.format(expense.payDate)));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 5px';
    td.style.width = '110px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(expense.payee));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 8px';
    td.style.width = '150px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(expense.particular));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 8px';
    td.style.width = '75px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode('$' + expense.amount));
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.width = '120px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(expense.method));
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.width = '82px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(expense.chequeNumber));
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.width = '24px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode((expense.ng) ? 'Y' : 'N'));
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.width = '24px';
    td.style.padding = '0 0 0 2px';
    if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode((expense.ne) ? 'Y' : 'N'));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 10px';
    td.style.width = '70px';
    
    if (expense.status == -1) {
      td.style.color = '#ff0000';
      if (expense.cancelledMessage && expense.cancelledMessage.length > 0) {
        td.appendChild(document.createTextNode('delete: ' + ((expense.cancelledMessage.length > 5) ? expense.cancelledMessage.substring(0, 5) + '...' : expense.cancelledMessage)));
        var a = document.createElement('a');
        a.href = '#';
        a._expense = expense;
        a.appendChild(document.createTextNode('All'));
        a.onclick = function() { func1 = function() { _self._popupBox2._close(); };
                                 var pos = DOMUtils.findPos(this);
                                 var div = document.createElement('div');
                                 div.style.verticalAlign = 'top';
                                 div.style.width = '420px';
                                 div.style.color = '#ff0000';
                                 div.style.textAlign = 'left';
                                 div.style.padding = '4px 0 0 10px';
                                 div.style.wordWrap = 'break-word';
                                 div.appendChild(MiscUtils.span('\n' + this._expense.cancelledMessage));
                                 MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, {t: 'Cancel Reason (' + this._expense.cancelledOperator + ', ' + SimpleDate.format(this._expense.cancelledDate) + ')', width: 460, height: 100});
                                 return false;
                               };
        td.appendChild(a);
      } else {
        td.appendChild(document.createTextNode(expense.cancelledMessage));
      }
    } else {
      if (expense.note.length > 10) {
        td.appendChild(document.createTextNode(expense.note.substring(0, 10) + '...'));
        var a = document.createElement('a');
        a.href = '#';
        a._expense = expense;
        a.appendChild(document.createTextNode('All'));
        a.onclick = function() { func1 = function() { _self._popupBox._close(); };
                                 var pos = DOMUtils.findPos(this);
                                 var div = document.createElement('div');
                                 div.appendChild(MiscUtils.span('\n' + this._expense.note));
                                 MiscUtils.dialog(_self._popupBox, pos, div, func1, null);
                                 return false;
                               };
        td.appendChild(a);
      } else {
        td.appendChild(document.createTextNode(expense.note));
        if (expense.note && expense.note.length == 0) {
          td.appendChild(document.createTextNode('(none)'));
        }
      }
    }
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 10px';
    td.style.width = '135px';
    if(expense.status == 0) {
      td.appendChild(document.createTextNode('未确认'));
    } else if (expense.status == 1) {
      td.appendChild(document.createTextNode('已确认'));
    } else if (expense.status == 2) {
      td.appendChild(document.createTextNode('已支付'));
    } else if (expense.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    
    var remove = document.createElement('img');
    remove.src = 'images/common/delete.png';
    remove.style.cursor = 'pointer';
    remove.style.margin = '0 0 0 50px';
    remove._expense = expense;
    remove.onclick = function() { var pos = DOMUtils.findPos(this);
                                  var div = document.createElement('div');
                                  var expense = this._expense
                                  var func1 = function() { expense.status = -1;
                                                           expense.cancelledDate = _self._now;
                                                           expense.cancelledOperator = _self._operator.account;
                                                           expense.cancelledMessage = expense._text;
                                                           new RequestUtils()._write('expense', [expense], [], function() { _self._retrieveResults(1); _self._retrieveExpenses(1); }, { pos: pos });
                                                           _self._popupBox2._close();
                                                         };
                                  var func2 = function() { _self._popupBox2._close(); };
                                  MiscUtils.dialog(_self._popupBox2, pos, div, func1, func2, {t: 'Cancel Reason'});
                                  new DialogInput(div, expense, { title: '' });
                                  return false;
                                };
    if (expense.status != -1) {
      td.appendChild(remove);
    }
  }
  Pagination.makePagedResults(this._gui.pagination, this._parameters.page, this._parameters.total, this._parameters.size, function(page, condition) { _self._retrieveExpenses.call(_self, page, _self._parameters.condition); }, this, document);
  
  this._expenseSearch(this._gui, function(condition, from, to, pfrom, pto, pos) { _self._retrieveResults.call(_self, 1, condition, from, to, pfrom, pto, pos); _self._retrieveExpenses.call(_self, 1, condition, from, to, pfrom, pto, pos); });
};

HandlePageAcctExpense.prototype._expenseSearch = function(gui, callbackFunc) {
  var _self = this;
  this._callbackFunc = callbackFunc;
  gui.search.cdateFrom.value = (this._search.cdateFrom) ? SimpleDate.format(this._search.cdateFrom) : '';
  gui.search.cdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.cdateFrom, function(sd) { _self._search.cdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.cdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.cdateTo.value = (this._search.cdateTo) ? SimpleDate.format(this._search.cdateTo) : '';
  gui.search.cdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.cdateTo, function(sd) { _self._search.cdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  gui.search.cdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
                                             
  gui.search.pdateFrom.value = (this._search.pdateFrom) ? SimpleDate.format(this._search.pdateFrom) : '';
  gui.search.pdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.pdateFrom, function(sd) { _self._search.pdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.pdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.pdateTo.value = (this._search.pdateTo) ? SimpleDate.format(this._search.pdateTo) : '';
  gui.search.pdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.pdateTo, function(sd) { _self._search.pdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  gui.search.pdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
  gui.search.payee.options[gui.search.payee.options.length] = new Option('');
  for (var i = 0, il = this._payees.length; i < il; i++) {
    var payee = this._payees[i];
    var option = new Option(payee.name);
    gui.search.payee.options[gui.search.payee.options.length] = option;
    if (option.text == this._search.payee){
      gui.search.payee.selectedIndex = gui.search.payee.options.length - 1;
    }
  }
  if (this._payees.length > 0) {
    this._search.payee = gui.search.payee.options[gui.search.payee.selectedIndex].text;
    gui.search.payee.onchange = function() { _self._search.payee = this.options[this.selectedIndex].text; };
  }
  
  gui.search.particular.options[gui.search.particular.options.length] = new Option('');
  for (var i = 0, il = this._particulars.length; i < il; i++) {
    var particular = this._particulars[i];
    var option = new Option(particular.name);
    gui.search.particular.options[gui.search.particular.options.length] = option;
    if (option.text == this._search.payee){
      gui.search.particular.selectedIndex = gui.search.particular.options.length - 1;
    }
  }
  if (this._particulars.length > 0) {
    this._search.particular = gui.search.particular.options[gui.search.particular.selectedIndex].text;
    gui.search.particular.onchange = function() { _self._search.particular = this.options[this.selectedIndex].text; };
  }
  
  gui.search.amount.value = this._search.amount;
  gui.search.amount.onkeydown = function() { _self._search.amount = this.value; };
  gui.search.amount.onchange = function() { _self._search.amount = this.value; };
  gui.search.amount.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                               if (code == 13) {
                                                 _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
  gui.search.method.options[gui.search.method.options.length] = new Option('');
  for (var i = 0, il = ExpenseMethod.array().length; i < il; i++) {
    var method = ExpenseMethod.array()[i];
    var option = new Option(method);
    gui.search.method.options[gui.search.method.options.length] = option;
    if (option.text == this._search.method){
      gui.search.method.selectedIndex = gui.search.method.options.length - 1;
    }
  }
  if (ExpenseMethod.array().length > 0) {
    this._search.method = (gui.search.method.options[gui.search.method.selectedIndex].text == '{{$smarty.const.EXPENSE_METHOD_CASH|escape:'javascript'}}') ? 'C' : (gui.search.method.options[gui.search.method.selectedIndex].text == '{{$smarty.const.EXPENSE_METHOD_BANK|escape:'javascript'}}') ? 'B' : (gui.search.method.options[gui.search.method.selectedIndex].text == '{{$smarty.const.EXPENSE_METHOD_CHEQUE|escape:'javascript'}}') ? 'Q' : '';
    gui.search.method.onchange = function() { _self._search.method = (this.options[this.selectedIndex].text == '{{$smarty.const.EXPENSE_METHOD_CASH|escape:'javascript'}}') ? 'C' : (this.options[this.selectedIndex].text == '{{$smarty.const.EXPENSE_METHOD_BANK|escape:'javascript'}}') ? 'B' : (this.options[this.selectedIndex].text == '{{$smarty.const.EXPENSE_METHOD_CHEQUE|escape:'javascript'}}') ? 'Q' : ''; };
  }

  this._search.chequeNo = gui.search.chequeNo.value;
  gui.search.chequeNo.value = this._search.chequeNo;
  gui.search.chequeNo.onkeydown = function() { _self._search.chequeNo = this.value; };
  gui.search.chequeNo.onchange = function() { _self._search.chequeNo = this.value; };
  gui.search.chequeNo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                              if (code == 13) {
                                                _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                              }
                                            };
  gui.search.chequeNo.onkeyup = function() { if (gui.search.chequeNo.value.length == 0) {
                                           _self._popupBox._close();
                                         } else {
                                           var div = document.createElement('div');
                                           var pos1 = DOMUtils.findPos(gui.search.chequeNo);
                                           var pos = [pos1[0] + 1, pos1[1] + 82];
                                           var query = "SELECT e.chequeNumber FROM ns_Expense as e WHERE 1 = 1 AND " + '((e.chequeNumber LIKE \'%' + gui.search.chequeNo.value + '%\'))' + " ";
                                           MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                           new DialogSearchResult(div, gui.search.chequeNo, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();}, width: 195});
                                         }
                                       };
 this._search.note = gui.search.note.value;
 gui.search.note.value = this._search.note;
 gui.search.note.onkeydown = function() { _self._search.note = this.value; };
 gui.search.note.onchange = function() { _self._search.note = this.value; };
 gui.search.note.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                             if (code == 13) {
                                               _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                             }
                                           };
 gui.search.note.onkeyup = function() { if (gui.search.note.value.length == 0) {
                                          _self._popupBox._close();
                                        } else {
                                          var div = document.createElement('div');
                                          var pos1 = DOMUtils.findPos(gui.search.note);
                                          var pos = [pos1[0] + 1, pos1[1] + 82];
                                          var query = "SELECT e.note FROM ns_Expense as e WHERE 1 = 1 AND " + '((e.note LIKE \'%' + gui.search.note.value + '%\'))' + " ";
                                          MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                          new DialogSearchResult(div, gui.search.note, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();}, width: 195});
                                        }
                                      };
  gui.search.ng.defaultChecked = this._search.ng;
  gui.search.ng.onclick = function() { if (this.checked == true) {
                                         _self._search.ng = true;
                                       } else {
                                         _self._search.ng = false;
                                       }
                                     };
  gui.search.ne.defaultChecked = this._search.ne;
  gui.search.ne.onclick = function() { if (this.checked == true) {
                                         _self._search.ne = true;
                                       } else {
                                         _self._search.ne = false;
                                       }
                                     };
  gui.search.paid.defaultChecked = this._search.paid;
  gui.search.paid.onclick = function() { if (this.checked == true) {
                                           _self._search.paid = true;
                                         } else {
                                           _self._search.ne = false;
                                         }
                                       };
  gui.search.unpaid.defaultChecked = this._search.unpaid;
  gui.search.unpaid.onclick = function() { if (this.checked == true) {
                                             _self._search.unpaid = true;
                                           } else {
                                             _self._search.unpaid = false;
                                           }
                                         };
  gui.search.search.onclick = function() { _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                         };
};

HandlePageAcctExpense.prototype._toString = function(search) {
  var str = '1 = 1';
  
  str += (search.payee == '') ? '' : ' AND (d.payee LIKE \'%' + search.payee + '%\')';
  str += (search.particular == '') ? '' : ' AND (d.particular LIKE \'%' + search.particular + '%\')';
  str += (search.amount == '') ? '' : ((isNaN(search.amount)) ? ' AND 1 = 0' : ' AND (d.amount=\'' + search.amount + '\')');
  str += (search.method == '') ? '' : ' AND (d.method=\'' + search.method + '\')';
  str += (search.chequeNo == '') ? '' : ' AND (d.chequeNumber LIKE \'%' + search.chequeNo + '%\')';
  str += (search.note == '') ? '' : ' AND (d.note LIKE \'%' + search.note + '%\')';
  
  if (search.ng && search.ne && search.paid && search.unpaid) {
    str += ' AND ((d.ng = 1) AND (d.ne = 1) AND ((d.status = 2) OR (d.status = 1)))';
  } else if (search.ng && search.ne && search.paid) {
    str += ' AND ((d.ng = 1) AND (d.ne = 1) AND (d.status = 2))';
  } else if (search.ng && search.ne && search.unpaid) {
    str += ' AND ((d.ng = 1) AND (d.ne = 1) AND (d.status = 1))';
  } else if (search.ng && search.paid && search.unpaid) {
    str += ' AND ((d.ng = 1) AND ((d.status = 2) OR (d.status = 1)))';
  } else if (search.ne && search.paid && search.unpaid) {
    str += ' AND ((d.ne = 1) AND ((d.status = 2) OR (d.status = 1)))';
  } else if (search.ng && search.ne) {
    str += ' AND ((d.ng = 1) AND (d.ne = 1))';
  } else if (search.ng && search.paid) {
    str += ' AND ((d.ng = 1) AND (d.status = 2))';
  } else if (search.ng && search.unpaid) {
    str += ' AND ((d.ng = 1) AND (d.status = 1))';
  } else if (search.ne && search.paid) {
    str += ' AND ((d.ne = 1) AND (d.status = 2))';
  } else if (search.ne && search.unpaid) {
    str += ' AND ((d.ne = 1) AND (d.status = 1))';
  } else if (search.paid && search.unpaid) {
    str += ' AND ((d.status = 2) OR (d.status = 1))';
  } else if (search.ng) {
    str += ' AND (d.ng = 1)';
  }  else if (search.ne) {
    str += ' AND (d.ne = 1)';
  } else if (search.paid) {
    str += ' AND (d.status = 2)';
  } else if (search.unpaid) {
    str += ' AND (d.status = 1)';
  }
  
  return str;
};

HandlePageAcctExpense.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePageAcctExpense.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePageAcctExpense.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};